package com.net.wanjian.networkhospitalmanager.fragment;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.MyQRcodeActivity;
import com.net.wanjian.networkhospitalmanager.activity.ScanPortraitActivity;
import com.net.wanjian.networkhospitalmanager.activity.TotateOrderActivity;
import com.net.wanjian.networkhospitalmanager.activity.evaluate.HistoryEventEvaluateActivity;
import com.net.wanjian.networkhospitalmanager.activity.my.NotificationListActivity;
import com.net.wanjian.networkhospitalmanager.activity.my.PersonalInfoActivity;
import com.net.wanjian.networkhospitalmanager.activity.my.SettingActivity;
import com.net.wanjian.networkhospitalmanager.bean.UserInfoReturn;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.fragment.base.BaseFragment;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.DensityToPxUtil;
import com.net.wanjian.networkhospitalmanager.utils.PicassoUtil;
import com.net.wanjian.networkhospitalmanager.utils.QRCodeUtil;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.net.wanjian.networkhospitalmanager.view.QRcodeDialog;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

/**
 * Description:我的
 * Created by lidongcai on 2017/4/22.
 * Email:  lidongcaiocc@163.com
 */
public class MyFragment extends BaseFragment implements View.OnClickListener {


    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.top_title_tv)
    TextView topTitleTv;
    @BindView(R.id.my_personal_info_layout)
    RelativeLayout myPersonalInfoLayout;
    @BindView(R.id.my_message_notify_layout)
    RelativeLayout myMessageNotifyLayout;
    @BindView(R.id.my_qr_code_layout)
    RelativeLayout myQrCodeLayout;
    @BindView(R.id.my_event_ing_layout)
    RelativeLayout myEventIngLayout;
    @BindView(R.id.my_relation_info_layout)
    RelativeLayout myRelationInfoLayout;
    @BindView(R.id.my_setting_layout)
    RelativeLayout mySettingLayout;
    @BindView(R.id.my_head_image_iv)
    ImageView myHeadImageIv;
    @BindView(R.id.my_user_name_tv)
    TextView myUserNameTv;
    @BindView(R.id.my_num_tv)
    TextView myNumTv;
    @BindView(R.id.my_message_layout)
    RelativeLayout messageRelativeLayout;
    @BindView(R.id.my_totate_order_layout)
    RelativeLayout myTotateOrderLayout;
    @BindView(R.id.qr_code_layout)
    RelativeLayout qrCodeLayout;




    private UserInfoReturn mUserInfoReturn;

    @Override
    protected void initView() {
        topTitleTv.setText("我的");
        topBackLayout.setVisibility(View.GONE);
        qrCodeLayout.setOnClickListener(this);
        myTotateOrderLayout.setOnClickListener(this);
        messageRelativeLayout.setOnClickListener(this);
        myPersonalInfoLayout.setOnClickListener(this);
        myMessageNotifyLayout.setOnClickListener(this);
        myQrCodeLayout.setOnClickListener(this);
        myEventIngLayout.setOnClickListener(this);
        myRelationInfoLayout.setOnClickListener(this);
        mySettingLayout.setOnClickListener(this);

        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();

        PicassoUtil.loadImage(mContext,
                HttpUtil.getImageUrl(xmlUtil.getHospitalId(), xmlUtil.getUserInfoId(), 2),
                R.mipmap.user, myHeadImageIv);

        if (xmlUtil.getUserIdentity().equals("1")) {
            myTotateOrderLayout.setVisibility(View.GONE);
        }

        getUserInfoHttpRequest(xmlUtil);
    }

    private void getUserInfoHttpRequest(SharedXmlUtil xmlUtil) {
        HttpUtil.getUserInfo(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(), xmlUtil.getUserInfoId(), new DisposableObserver<UserInfoReturn>() {
            @Override
            public void onNext(UserInfoReturn userInfoReturn) {
                if (Integer.parseInt(userInfoReturn.getResult()) == ErrorCodeReturn.success) {
                    SharedXmlUtil.getInstance().setToken(userInfoReturn.getData().getToken());
                    mUserInfoReturn = userInfoReturn;
                    setDataUI(userInfoReturn);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void setDataUI(UserInfoReturn userInfoReturn) {

        myUserNameTv.setText(URLDecoderUtil.getDecoder(userInfoReturn.getData().getUserInfo().getUserInfoTrueName()));
        myNumTv.setText("学号：" + URLDecoderUtil.getDecoder(userInfoReturn.getData().getUserInfo().getUserInfoCode()));
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    public static MyFragment getInstance() {
        MyFragment myFragment = new MyFragment();
        return myFragment;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_personal_info_layout:
                Bundle bundle = new Bundle();
                bundle.putSerializable(PersonalInfoActivity.USER_INFO_INTENT_KEY, mUserInfoReturn);
                openActivity(PersonalInfoActivity.class, bundle);
                break;
            case R.id.my_message_notify_layout:
                break;
            case R.id.my_qr_code_layout://二维码
                String number = URLDecoderUtil.getDecoder(mUserInfoReturn.getData().getUserInfo().getUserInfoCode());
                String trueName = URLDecoderUtil.getDecoder(mUserInfoReturn.getData().getUserInfo().getUserInfoTrueName());
                Bundle bundle1 = new Bundle();
                bundle1.putString("number", number);
                bundle1.putString("trueName", trueName);
                openActivity(MyQRcodeActivity.class, bundle1);
//                showQRcode("WANJIAN^USER^"+SharedXmlUtil.getInstance().read(SharedPreferencesKeyConst.EncryptUser_Identity_ID_key, ""));
                break;
            case R.id.my_event_ing_layout://历史预约事件
                openActivity(HistoryEventEvaluateActivity.class);
//                openActivity(HistoryReserveListActivity.class);
                break;
            case R.id.my_relation_info_layout://用户信息
                break;
            case R.id.my_totate_order_layout://轮转排班
                openActivity(TotateOrderActivity.class);
                break;
            case R.id.my_message_layout://通知列表
                openActivity(NotificationListActivity.class);
                break;
            case R.id.my_setting_layout://设置
                openActivity(SettingActivity.class);
                break;
            case R.id.qr_code_layout://扫描二维码
                scanQrCode();
                break;
        }
    }

    private void scanQrCode() {
        IntentIntegrator integrator = new IntentIntegrator(getActivity());
        integrator.setPrompt("请扫描二维码");
        integrator.setCaptureActivity(ScanPortraitActivity.class);
        integrator.initiateScan();
    }

    private void showQRcode(String key) {
        Bitmap decodeResource = BitmapFactory.decodeResource(getResources(), R.mipmap.infigure_png);
        Bitmap qrBitmap = QRCodeUtil.createQRImage(key, DensityToPxUtil.dp2px(mContext, 190), DensityToPxUtil.dp2px(mContext, 190), decodeResource);
        QRcodeDialog.Builder builder = new QRcodeDialog.Builder(mContext);
        QRcodeDialog qRcodeDialog = builder.setBitmap(qrBitmap)
                .setHeaderImageUrl(HttpUtil.getImageUrl(SharedXmlUtil.getInstance().getHospitalId(), SharedXmlUtil.getInstance().getUserInfoId(), 2))
                .setNumber(URLDecoderUtil.getDecoder(mUserInfoReturn.getData().getUserInfo().getUserInfoCode()))
                .setUsername(URLDecoderUtil.getDecoder(mUserInfoReturn.getData().getUserInfo().getUserInfoTrueName()))
                .create();
        qRcodeDialog.setCanceledOnTouchOutside(true);
        qRcodeDialog.show();
    }
}
